using System.Collections.Generic;
using SimpleJSON;
using UnityEngine;

namespace Gj
{
    public static class MapTagTools
    {
        private static readonly Dictionary<string, MapTagSettings.TagItem> map;

        static MapTagTools()
        {
            var settings = LoadSettings();
            map = new Dictionary<string, MapTagSettings.TagItem>();
            for (var i = 0; i < settings.TagItems.Length; i++)
            {
                var tagItem = settings.TagItems[i];
                map.Add(tagItem.id, tagItem);
            }
        }

        private static MapTagSettings LoadSettings()
        {
            var loadedSettings = (MapTagSettings)Resources.Load("MapTagSettings", typeof(MapTagSettings));
            if (loadedSettings == null)
            {
                loadedSettings = ScriptableObject.CreateInstance<MapTagSettings>();
                Debug.LogError("MapTagSettings is not exist (Resources)");
            }

            return loadedSettings;
        }

        public static MapTagSettings.TagItem Get(string id)
        {
            if (map.ContainsKey(id)) return map[id];
            return new MapTagSettings.TagItem();
        }

        public static JSONArray GetAll()
        {
            var settings = LoadSettings();
            var list = new JSONArray() { };
            for (var i = 0; i < settings.TagItems.Length; i++)
            {
                list.Add(new JSONObject(){ { "id", settings.TagItems[i].id }, { "label", settings.TagItems[i].label }, { "show", settings.TagItems[i].show } });
            }

            return list;
        }
    }
}